﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data;
using System.Collections;

namespace DAO
{
    public class HotelDAO
    {
        public List<LoaiPhongDTO> layDSLoaiPhong(string wsLink, string username, string password)
        {
            try
            {
                G09HotelWebService.Service client = new G09HotelWebService.Service();
                client.Url = wsLink;
                client.CookieContainer = new System.Net.CookieContainer();
                client.DangNhap(username, password);
                G09HotelWebService.LoaiPhong[] loaiPhongArr = client.LayDSLoaiPhong();
                List<LoaiPhongDTO> loaiPhongLst = new List<LoaiPhongDTO>();
                for (int i = 0; i < loaiPhongArr.Length; i++)
                {
                    LoaiPhongDTO lp = new LoaiPhongDTO();
                    lp.MaLoaiPhong = loaiPhongArr[i].MaLoaiPhong.ToString();
                    lp.MoTa = loaiPhongArr[i].MoTa;
                    lp.SoNguoi = loaiPhongArr[i].SoNguoi;
                    lp.TenLoaiPhong = loaiPhongArr[i].TenLoaiPhong;
                    loaiPhongLst.Add(lp);
                }
                return loaiPhongLst;
            }
            catch (Exception ex)
            {
                throw ex;
            }


        }

        public List<HangPhongDTO> layDSHangPhong(string wsLink, string username, string password)
        {
            try
            {
                G09HotelWebService.Service client = new G09HotelWebService.Service();
                client.Url = wsLink;
                client.CookieContainer = new System.Net.CookieContainer();
                client.DangNhap(username, password);
                List<HangPhongDTO> hangPhongLst = new List<HangPhongDTO>();
                G09HotelWebService.HangPhong[] hpArr = client.LayDSHangPhong();
                for (int i = 0; i < hpArr.Length; i++)
                {
                    HangPhongDTO hp = new HangPhongDTO();
                    hp.MaHangPhong = hpArr[i].MaHangPhong.ToString();
                    hp.MoTa = hpArr[i].MoTa;
                    hp.TenHangPhong = hpArr[i].TenHangPhong;
                    hangPhongLst.Add(hp);
                }
                return hangPhongLst;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public List<RoomDTO> timKiemPhong(string wsLink, string username, string password, string loaiPhong, string hangPhong)
        {
            try
            {
                G09HotelWebService.Service client = new G09HotelWebService.Service();
                client.Url = wsLink;
                client.CookieContainer = new System.Net.CookieContainer();
                client.DangNhap(username, password);
                G09HotelWebService.LoaiPhong[] lpArr = client.LayDSLoaiPhongTheoHangPhong(int.Parse(hangPhong));
                List<RoomDTO> roomLst = new List<RoomDTO>();

                //lay thog tin hang phong
                G09HotelWebService.HangPhong[] hpArr = client.LayDSHangPhong();
                HangPhongDTO hp = new HangPhongDTO();
                for (int i = 0; i < hpArr.Length; i++)
                {
                    if (hpArr[i].MaHangPhong.CompareTo(int.Parse(hangPhong)) == 0)
                    {
                        hp.MaHangPhong = hpArr[i].MaHangPhong.ToString();
                        hp.MoTa = hpArr[i].MoTa;
                        hp.TenHangPhong = hpArr[i].TenHangPhong;
                    }
                }
                if (hp.MaHangPhong == null)
                {
                    throw new Exception("Khong ton tai hang phong nay");
                }

                //tim loai phong theo hang phong, va lay gia
                if (loaiPhong != "")
                {
                    for (int i = 0; i < lpArr.Length; i++)
                    {
                        if (lpArr[i].MaLoaiPhong.ToString().CompareTo(loaiPhong) == 0)
                        {
                            RoomDTO room = new RoomDTO();
                            room.HangPhong = hp;
                            room.LoaiPhong = new LoaiPhongDTO();
                            room.LoaiPhong.MaLoaiPhong = lpArr[i].MaLoaiPhong.ToString();
                            room.LoaiPhong.MoTa = lpArr[i].MoTa;
                            room.LoaiPhong.SoNguoi = lpArr[i].SoNguoi;
                            room.LoaiPhong.TenLoaiPhong = lpArr[i].TenLoaiPhong;
                            room.Gia = client.LayGiaThanh(int.Parse(loaiPhong), int.Parse(hangPhong));
                            roomLst.Add(room);
                            return roomLst;
                        }
                    }
                    return roomLst;
                }
                else
                {
                    for (int i = 0; i < lpArr.Length; i++)
                    {
                        RoomDTO room = new RoomDTO();
                        room.HangPhong = hp;
                        room.LoaiPhong = new LoaiPhongDTO();
                        room.LoaiPhong.MaLoaiPhong = lpArr[i].MaLoaiPhong.ToString();
                        room.LoaiPhong.MoTa = lpArr[i].MoTa;
                        room.LoaiPhong.SoNguoi = lpArr[i].SoNguoi;
                        room.LoaiPhong.TenLoaiPhong = lpArr[i].TenLoaiPhong;
                        room.Gia = client.LayGiaThanh(lpArr[i].MaLoaiPhong, int.Parse(hangPhong));
                        roomLst.Add(room);
                    }
                    return roomLst;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public List<RoomDTO> timKiemPhongMoRong(string wsLink, string username, string password, DateTime fromDate, DateTime toDate)
        {
            try
            {
                G09HotelWebService.Service client = new G09HotelWebService.Service();
                client.Url = wsLink;
                client.CookieContainer = new System.Net.CookieContainer();
                client.DangNhap(username, password);
                G09HotelWebService.LoaiHangPhong[] lhpArr = client.LayDSLoaiHangPhongHoTro(fromDate, toDate);
                G09HotelWebService.HangPhong[] hpArr = client.LayDSHangPhong();
                G09HotelWebService.LoaiPhong[] lpArr = client.LayDSLoaiPhong();
                List<RoomDTO> roomLst = new List<RoomDTO>();

                for (int i = 0; i < lhpArr.Length; i++)
                {
                    RoomDTO room = new RoomDTO();
                    G09HotelWebService.HangPhong hp = this.layHangPhong(lhpArr[i].MaHangPhong, hpArr);
                    room.HangPhong = new HangPhongDTO();
                    room.LoaiPhong = new LoaiPhongDTO();
                    room.HangPhong.MaHangPhong = hp.MaHangPhong.ToString();
                    room.HangPhong.MoTa = hp.MoTa;
                    room.HangPhong.TenHangPhong = hp.TenHangPhong;
                    G09HotelWebService.LoaiPhong lp = this.layLoaiPhong(lhpArr[i].MaLoaiPhong, lpArr);
                    room.LoaiPhong.MaLoaiPhong = lp.MaLoaiPhong.ToString();
                    room.LoaiPhong.MoTa = lp.MoTa;
                    room.LoaiPhong.SoNguoi = lp.SoNguoi;
                    room.LoaiPhong.TenLoaiPhong = lp.TenLoaiPhong;
                    room.Gia = lhpArr[i].GiaPhong;
                    room.SoChoTrong = lhpArr[i].SoLuong;
                    roomLst.Add(room);
                }
                return roomLst;

            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public G09HotelWebService.HangPhong layHangPhong(int ma, G09HotelWebService.HangPhong[] hangPhongArr)
        {
            for (int i = 0; i < hangPhongArr.Length; i++)
            {
                if (hangPhongArr[i].MaHangPhong.CompareTo(ma) == 0)
                    return hangPhongArr[i];
            }
            return null;
        }

        public G09HotelWebService.LoaiPhong layLoaiPhong(int ma, G09HotelWebService.LoaiPhong[] loaiPhongArr)
        {
            for (int i = 0; i < loaiPhongArr.Length; i++)
            {
                if (loaiPhongArr[i].MaLoaiPhong.CompareTo(ma) == 0)
                    return loaiPhongArr[i];
            }
            return null;
        }

        public int layGiaPhong(string wsLink, string username, string password, string loaiphong, string hangphong)
        {
            try
            {
                G09HotelWebService.Service client = new G09HotelWebService.Service();
                client.Url = wsLink;
                client.CookieContainer = new System.Net.CookieContainer();
                client.DangNhap(username, password);
                return client.LayGiaThanh(int.Parse(loaiphong), int.Parse(hangphong));
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public HangPhongDTO layHangPhong(string wsLink, string username, string password, string maHangPhong)
        {
            try
            {
                List<HangPhongDTO> hpLst = this.layDSHangPhong(wsLink, username, password);
                if (hpLst == null)
                {
                    return null;
                }
                for (int i = 0; i < hpLst.Count; i++)
                {
                    if (hpLst[i].MaHangPhong.CompareTo(maHangPhong) == 0)
                    {
                        return hpLst[i];
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        public LoaiPhongDTO layLoaiPhong(string wsLink, string username, string password, string maLoaiPhong)
        {
            try
            {
                List<LoaiPhongDTO> lpLst = this.layDSLoaiPhong(wsLink, username, password);
                if (lpLst == null)
                {
                    return null;
                }
                for (int i = 0; i < lpLst.Count; i++)
                {
                    if (lpLst[i].MaLoaiPhong.CompareTo(maLoaiPhong) == 0)
                    {
                        return lpLst[i];
                    }
                }
                return null;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        /// <summary>
        /// Dat Phong
        /// </summary>
        /// <param name="wsLink"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <param name="ttdp"></param>
        /// <returns></returns>
        /// <proc>sp_HoaDon_datPhongKhachSan</proc>
        public string datPhong(string wsLink, string username, string password, ThongTinDatPhong ttdp)
        {
            try
            {
                //dat ben webservice
                G09HotelWebService.Service client = new G09HotelWebService.Service();
                client.Url = wsLink;
                client.CookieContainer = new System.Net.CookieContainer();
                client.DangNhap(username, password);
                bool res = client.DatPhong(int.Parse(ttdp.LoaiPhong.MaLoaiPhong), int.Parse(ttdp.HangPhong.MaHangPhong), ttdp.NgayBatDau, ttdp.NgayKetThuc, ttdp.SoLuongPhong);                
                if (!res)
                {
                    return null;
                }

                //ghi nhan vao csdl
                List<string> paraLst = new List<string>();
                ArrayList valArr = new ArrayList();
                //check lai cho ma hoa don ben ncc
                paraLst.Add("@mahdbenncc");
                valArr.Add("");
                ttdp.LoaiPhong.MaLoaiPhong = "0";
                ttdp.HangPhong.MaHangPhong = "0";
                ttdp.LoaiPhong.TenLoaiPhong = "dch";
                ttdp.HangPhong.TenHangPhong = "dcl";

                paraLst.Add("@ngayHetHanTT");
                valArr.Add(ttdp.NgayBatDau.AddDays(-3));
                paraLst.Add("@tongSoLuong");
                valArr.Add(ttdp.SoLuongPhong);
                paraLst.Add("@tongTien");
                valArr.Add(ttdp.ThanhTien);
                paraLst.Add("@donViTinh");
                valArr.Add(ttdp.NhaCungCap.DonViTinh);
                paraLst.Add("@pttt");
                valArr.Add(ttdp.HinhThucThanhToan);
                paraLst.Add("@makh");
                valArr.Add(ttdp.MaKH);
                paraLst.Add("@mancc");
                valArr.Add(ttdp.NhaCungCap.MaNCC);
                paraLst.Add("@maloaiphong");
                valArr.Add(int.Parse(ttdp.LoaiPhong.MaLoaiPhong));
                paraLst.Add("@mahangphong");
                valArr.Add(int.Parse(ttdp.HangPhong.MaHangPhong));
                paraLst.Add("@tenlp");                
                valArr.Add(ttdp.LoaiPhong.TenLoaiPhong);
                paraLst.Add("@tenhp");
                valArr.Add(ttdp.HangPhong.TenHangPhong);
                paraLst.Add("@ngayBD");
                valArr.Add(ttdp.NgayBatDau);
                paraLst.Add("@ngayKT");
                valArr.Add(ttdp.NgayKetThuc);
                paraLst.Add("@soLuongPhong");
                valArr.Add(ttdp.SoLuongPhong);
                paraLst.Add("@giaPhong");
                valArr.Add(ttdp.GiaPhong);
                paraLst.Add("@heSoGia");
                valArr.Add(ttdp.NhaCungCap.HeSoGia);
                paraLst.Add("@hoTen");
                valArr.Add(ttdp.HoTen);
                paraLst.Add("@diaChi");
                valArr.Add(ttdp.DiaChi);
                paraLst.Add("@dienThoai");
                valArr.Add(ttdp.SDT);
                paraLst.Add("@email");
                valArr.Add(ttdp.Email);

                DataTable dt = DataProvider.ExecuteStoreProc("sp_HoaDon_datPhongKhachSan", paraLst, valArr);
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                String mahd = dt.Rows[0][0].ToString();
                if (mahd == "0")
                {
                    return null;
                }
                else
                {
                    return mahd;
                }

                //paraLst.Add("@mahdbencc"
            }
            catch(Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Them thong tin vao bang thanh toan khi khach hang thuc hien thanh toan
        /// </summary>
        /// <param name="mahd"></param>
        /// <param name="soTien"></param>
        /// <param name="donViTinh"></param>
        /// <param name="transID"></param>
        /// <proc>sp_ThongTinThanhToan_thanhToan</proc>
        public void thanhToan(string mahd, float soTien, string donViTinh, string transID)
        {
            List<string> paraLst = new List<string>();
            ArrayList valArr = new ArrayList();
            paraLst.Add("@soTien");
            valArr.Add(soTien);
            paraLst.Add("@donViTinh");
            valArr.Add(donViTinh);
            paraLst.Add("@transactionID");
            valArr.Add(transID);
            paraLst.Add("@maHD");
            valArr.Add(mahd);
            try
            {
                DataProvider.ExecuteStoreProc("sp_ThongTinThanhToan_thanhToan", paraLst, valArr);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
